using UnityEngine;
using UnityEngine.EventSystems;

public class CardDragHandler : MonoBehaviour, IDragHandler, IEndDragHandler
{
    private Card currentCard;
    private bool canExecute;
    
    private void Awake()
    {
        currentCard = GetComponent<Card>();
    }


    public void OnDrag(PointerEventData eventData)
    { 
        if(currentCard.side != ChessSide.Player1)
            return;
        if(Settings.isPlayerTurn == false)
            return;
        if(!currentCard.isAvailable)
            return;
        if(!Settings.canBeUsed)
            return;
        currentCard.isAnimating = true;
        Vector3 screenPos = new Vector3(eventData.position.x, eventData.position.y, 10f); // Z 轴根据需要调整
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
        
        transform.SetAsLastSibling();
        
        currentCard.transform.position = worldPos;

        canExecute = worldPos.x > -3;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if(currentCard.side != ChessSide.Player1)
            return;
        if(Settings.isPlayerTurn == false)
            return;
        if(!currentCard.isAvailable)
            return;
        if(!Settings.canBeUsed)
            return;

        if(canExecute)
        {
            _ = currentCard.UseCardAsync();

        }else
        {
            currentCard.RestCardTransform();
        }

        currentCard.isAnimating = false;
    }
}
